﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Hxsoft.AppV1._Controls
{
	/// <summary>
	/// 关联下拉列表控件。
	/// </summary>
	[ValidationProperty("Value")]
	public partial class CascadingDropDownList : Hxsoft.AppV1.UserControl
	{
		/// <summary>
		/// 值分隔符。
		/// </summary>
		protected const string ValueSeparator = "\n";

		/// <summary>
		/// 加载数据时的提示文本。
		/// </summary>
		protected string LoadingText
		{
			get { return Res.LoadingText; }
		}

		/// <summary>
		/// 自定义的提示文本。
		/// </summary>
		protected string CustomPromptText
		{
			get { return Res.DropDownList_PromptText; }
		}

		private LoadedValue<string> loadedValueContainerID;
		/// <summary>
		/// 存储值的容器Id。
		/// </summary>
		public string ValueContainerID
		{
			get
			{
				if (loadedValueContainerID.Loader == null)
					loadedValueContainerID.Loader = () => StringExtensions.ConvertFromEffectiveValueOrDefault(ViewState["ValueContainerID"], this.ClientID);
				return loadedValueContainerID.Value;
			}
			set
			{
				if (loadedValueContainerID.Setter == null)
					loadedValueContainerID.Setter = v => ViewState["ValueContainerID"] = v;
				loadedValueContainerID.Value = value;
			}
		}

		/// <summary>
		/// 值。
		/// </summary>
		public string Value
		{
			get
			{
				return CtrSelectorValue.Value;
			}
			set
			{
				CtrSelectorValue.Value = value;
			}
		}

		private LoadedValue<string[]> loadedValues;
		/// <summary>
		/// 值数组。
		/// </summary>
		public string[] Values
		{
			get
			{
				if (loadedValues.Loader == null)
					loadedValues.Loader = () =>
						{
							string[] values = CtrSelectorValues.Value.Split(ValueSeparator);
							if (values == null || values.Length == 0) return values;
							for (int i = 0; i < values.Length; i++)
								values[i] = values[i].Trim(new char[]{'\r'});
							return values;
						};
				return loadedValues.Value;
			}
			set
			{
				if (loadedValues.Setter == null)
					loadedValues.Setter = v => CtrSelectorValues.Value = v.Join(ValueSeparator);
				loadedValues.Value = value;
			}
		}

		private LoadedValue<bool> loadedOnlySelectLeaf;
		/// <summary>
		/// 仅允许选择叶节点。
		/// </summary>
		public bool OnlySelectLeaf
		{
			get
			{
				if (loadedOnlySelectLeaf.Loader == null)
					loadedOnlySelectLeaf.Loader = () => BooleanExtensions.ConvertFromOrDefault(ViewState["OnlySelectLeaf"], false);
				return loadedOnlySelectLeaf.Value;
			}
			set
			{
				if (loadedOnlySelectLeaf.Setter == null)
					loadedOnlySelectLeaf.Setter = v => ViewState["OnlySelectLeaf"] = v;
				loadedOnlySelectLeaf.Value = value;
			}
		}

		private LoadedValue<int> loadedInitSelectorAmount;
		/// <summary>
		/// 初始选择器的数量。
		/// </summary>
		public int InitSelectorAmount
		{
			get
			{
				if (loadedInitSelectorAmount.Loader == null)
					loadedInitSelectorAmount.Loader = () => IntExtensions.ConvertFromOrDefault(ViewState["InitSelectorAmount"], 1);
				return loadedInitSelectorAmount.Value;
			}
			set
			{
				if (loadedInitSelectorAmount.Setter == null)
					loadedInitSelectorAmount.Setter = v => ViewState["InitSelectorAmount"] = v;
				loadedInitSelectorAmount.Value = value;
			}
		}

		private LoadedValue<string> loadedLoadDataUrl;
		/// <summary>
		/// 加载数据的Url。
		/// </summary>
		public string LoadDataUrl
		{
			get
			{
				if (loadedLoadDataUrl.Loader == null)
					loadedLoadDataUrl.Loader = () => (string)ViewState["LoadDataUrl"];
				return loadedLoadDataUrl.Value;
			}
			set
			{
				if (loadedLoadDataUrl.Setter == null)
					loadedLoadDataUrl.Setter = v => ViewState["LoadDataUrl"] = v;
				loadedLoadDataUrl.Value = value;
			}
		}

		private LoadedValue<string[]> loadedPromptTexts;
		/// <summary>
		/// 提示文本数组。
		/// </summary>
		public string[] PromptTexts
		{
			get
			{
				if (loadedPromptTexts.Loader == null)
					loadedPromptTexts.Loader = () => (string[])ViewState["PromptTexts"];
				return loadedPromptTexts.Value;
			}
			set
			{
				if (loadedPromptTexts.Setter == null)
					loadedPromptTexts.Setter = v => ViewState["PromptTexts"] = v;
				loadedPromptTexts.Value = value;
			}
		}



		protected override void OnPreRender(EventArgs e)
		{
			this.ApplicationPage.RegisterHeadIncludeFile(ApplicationConfiguration.JQueryUrl, IncludeFileType.JS);
			this.ApplicationPage.RegisterHeadIncludeFile(ApplicationConfiguration.CascadingDropDownListScriptUrl, IncludeFileType.JS);

			ScriptManager.RegisterStartupScript(this, this.GetType(), this.ID + "_Init", "$(" + this.ClientID + "_Init);", true);

			base.OnPreRender(e);
		}
	}
}